//
//  MoviePostView.m
//  MyMovie
//
//  Created by apple on 16/5/17.
//  Copyright © 2016年 ljp. All rights reserved.
//

#import "MoviePostView.h"
#import "MoviePostCollectionView.h"
#import "HeadMovieCollectionView.h"
#import "HeadMovieCollectionViewCell.h"

@interface MoviePostView ()
{
    UIView *headView;
    UIView *blackView;

    UIImageView *leftLightImageView;
    UIImageView *rightLightImageView;

    UIButton *button;

    UILabel *movieNameLable;

}
@end

@implementation MoviePostView

- (void)awakeFromNib {

    [self createSubviews];

    [self createCollectionView];
}

//创建单元格视图
- (void)createCollectionView {

    //创建下部单元格视图
    MoviePostCollectionView *moviePostCollectionView = [[MoviePostCollectionView alloc] initWithFrame:CGRectMake(0, 30, KScreenWidth, KScreenHeight-49-30-40) WithCellSize:CGSizeMake(((KScreenHeight-49-30-40-84)/1.7), KScreenHeight-49-30-40-84)];

    [moviePostCollectionView registerNib:[UINib nibWithNibName:@"MovieCellCollectionViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"MovieCellCollectionViewCell"];
    moviePostCollectionView.tag = 112233;
    [self insertSubview:moviePostCollectionView atIndex:0];

    //创建头部单元格视图
    HeadMovieCollectionView *headMovieCollectionView = [[HeadMovieCollectionView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, 100) WithCellSize:CGSizeMake(90/1.7, 90)];
    headMovieCollectionView.tag = 332211;
    [headMovieCollectionView registerClass:[HeadMovieCollectionViewCell class] forCellWithReuseIdentifier:@"HeadMovieCollectionViewCell"];
    [headView insertSubview:headMovieCollectionView atIndex:1];

    //添加键值观察者
    [moviePostCollectionView addObserver:self forKeyPath:@"currentIndex" options:NSKeyValueObservingOptionNew context:nil];
    [headMovieCollectionView addObserver:self forKeyPath:@"currentIndex" options:NSKeyValueObservingOptionNew context:nil];

}

- (void)createSubviews {

    headView = [[UIView alloc] initWithFrame:CGRectMake(0, -40, KScreenWidth, 130)];
    [self addSubview:headView];

    UIImageView *headBGImageView = [[UIImageView alloc] initWithFrame:headView.bounds];
    UIImage *image = [UIImage imageNamed:@"indexBG_home"];
    image = [image stretchableImageWithLeftCapWidth:0 topCapHeight:3];
    headBGImageView.image = image;
    [headView addSubview:headBGImageView];

    button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 0, 13, 10);
    button.center = CGPointMake(KScreenWidth/2, 120);
    [headView addSubview:button];
    headView.userInteractionEnabled = YES;
    [button setImage:[UIImage imageNamed:@"down_home"] forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:@"up_home"] forState:UIControlStateSelected];
    [button addTarget:self action:@selector(headViewMoveButton:) forControlEvents:UIControlEventTouchUpInside];

    blackView = [[UIView alloc] initWithFrame:self.bounds];
    blackView.backgroundColor = [UIColor blackColor];
    blackView.alpha = 0.5;
    blackView.hidden = YES;
    [self insertSubview:blackView belowSubview:headView];

    UIImage *lightImage = [UIImage imageNamed:@"light"];

    leftLightImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 20, 130)];
    leftLightImageView.center = CGPointMake(KScreenWidth/2-70, 0);
    leftLightImageView.top = 64;
    leftLightImageView.image = lightImage;
    leftLightImageView.hidden = YES;
    [self addSubview:leftLightImageView];

    rightLightImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 20, 130)];
    rightLightImageView.center = CGPointMake(KScreenWidth/2+70, 0);
    rightLightImageView.top = 64;
    rightLightImageView.image = lightImage;
    rightLightImageView.hidden = YES;
    [self addSubview:rightLightImageView];

    //添加手势
    UISwipeGestureRecognizer *swipeDown = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(headViewDown)];
    swipeDown.direction = UISwipeGestureRecognizerDirectionDown;
    [self addGestureRecognizer:swipeDown];

    UISwipeGestureRecognizer *swipeUp = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(headViewUp)];
    swipeUp.direction = UISwipeGestureRecognizerDirectionUp;
    [self addGestureRecognizer:swipeUp];



    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(headViewUp)];
    [blackView addGestureRecognizer:tap];

    movieNameLable = [[UILabel alloc] initWithFrame:CGRectMake(0, KScreenHeight-40-49, KScreenWidth, 40)];
    [self addSubview:movieNameLable];
    movieNameLable.textColor = [UIColor whiteColor];
    movieNameLable.backgroundColor = [UIColor blackColor];
    movieNameLable.textAlignment = NSTextAlignmentCenter;
    movieNameLable.text = @"蚁人";

}

- (void)headViewMoveButton:(UIButton *)button1 {

        if (button1.selected) {
            [self headViewUp];

        }else{
            [self headViewDown];
        }
}

- (void)headViewDown {

    [UIView animateWithDuration:0.4 animations:^{

    button.selected = YES;
    headView.top = 64;
    blackView.hidden = NO;
    leftLightImageView.hidden = NO;
    rightLightImageView.hidden = NO;
    }];
}

- (void)headViewUp {

    [UIView animateWithDuration:0.4 animations:^{

    button.selected = NO;
    headView.top = -40;
    blackView.hidden = YES;
    leftLightImageView.hidden = YES;
    rightLightImageView.hidden = YES;
    }];
}
//重写设置器方法,把数据给postCollectionView
-(void)setMovieArray:(NSArray *)movieArray {

    _movieArray = movieArray;

    MoviePostCollectionView *postCollectionView = [self viewWithTag:112233];
    postCollectionView.movieArray = movieArray;
    HeadMovieCollectionView *headMovieCollectionView = [self viewWithTag:332211];
    headMovieCollectionView.movieArray = movieArray;
}
//实现键值观察方法
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context {

    NSLog(@"keyPath=%@object=%@change=%@", keyPath, object, change);

    NSInteger itemIndex = [change[@"new"] integerValue];
    NSIndexPath *indexPath = [NSIndexPath indexPathForItem:itemIndex inSection:0];
    MoviePostCollectionView *postCollectionView = [self viewWithTag:112233];
    HeadMovieCollectionView *headMovieCollectionView = [self viewWithTag:332211];

    //改变电影名
    Movie *movie = self.movieArray[itemIndex];
    movieNameLable.text = movie.title;


    //判读是哪个视图变化,另一个单元格视图跟着滑动到指定位置
    if (object == postCollectionView && headMovieCollectionView.currentIndex != itemIndex) {

        [headMovieCollectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
        headMovieCollectionView.currentIndex = itemIndex;

    }else if (object == headMovieCollectionView && postCollectionView.currentIndex != itemIndex) {
        [postCollectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
        postCollectionView.currentIndex = itemIndex;
    }
}

@end
